//
//  CJStatusCell.swift
//  SinaWeibo
//
//  Created by 程建 on 16/9/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

//图片之间的间距
private let pictureCellMargin: CGFloat = 3

//计算图片的宽度
private let maxWidth:CGFloat = ScreenWidth - 2 * 8
private let itemWidth:CGFloat = (maxWidth - 2 * pictureCellMargin) / 3



class CJStatusCell: UITableViewCell {
    ///用户头像
    @IBOutlet weak var iconView: UIImageView!
    ///用户名称
    @IBOutlet weak var nameLabel: UILabel!
    ///用户等级
    @IBOutlet weak var memberImage: UIImageView!
    ///微博创建时间
    @IBOutlet weak var timeLabel: UILabel!
    ///微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    ///微博正文
    @IBOutlet weak var contentLabel: UILabel!
    ///微博认证
    @IBOutlet weak var avatarImage: UIImageView!
    
    ///配图视图
    @IBOutlet weak var pictureView: CJPictureView!
    ///配图视图的宽度约束对象
    @IBOutlet weak var pictureViewWidthCons: NSLayoutConstraint!
    ///配图视图的高度约束对象
    @IBOutlet weak var pictureViewHeightCons: NSLayoutConstraint!
    
    @IBOutlet weak var pictureLayout: UICollectionViewFlowLayout!
    
    //配图视图的顶部间距
    @IBOutlet weak var pictureViewTopCons: NSLayoutConstraint!
    
    @IBOutlet weak var repostBtn: UIButton!
    
    @IBOutlet weak var commentBtn: UIButton!
    
    @IBOutlet weak var ohYeah: UIButton!
   
    @IBOutlet weak var toolBar: UIView!
    
    
    //隐式的可选类型 实际上就是一个可选类型 不过在使用没有可选类型的特征
    //系统非常自信能够确保你在使用该控件的时候 该控件一定被实例化好了
    //被转发的原创微博的正文
    @IBOutlet weak var retweetedLabel: UILabel!
    
   
    
    
    
    //绑定数据
    var viewModel:CJStatusViewModel? {
        
        didSet{
        
            //时间
            timeLabel.text = viewModel?.timeText
            
            //来源
            sourceLabel.text = viewModel?.sourceText
            
            //正文
            contentLabel.text = viewModel?.status?.text
            
            //设置认证等级
            avatarImage.image = viewModel?.avatarImage
            
            //用户等级
            memberImage.image = viewModel?.memberImage
            
            //用户名称
            nameLabel.text = viewModel?.status?.user?.name
            
            //用户头像
            iconView .sd_setImage(with: viewModel?.iconURL)
            
            //获取配图视图的大小
            let count = viewModel?.pictureInfos?.count ?? 0
            let value = changePictureViewSize(Count: count)
            
            //更新约束值
            pictureViewWidthCons.constant = value.pSize.width
            
            pictureViewHeightCons.constant = value.pSize.height
            
            pictureLayout.itemSize = value.itemSize
            
            //给配图视图设置数据源
            pictureView.pictureInfos = viewModel?.pictureInfos
            
            //根据是否有配图调整顶部间距
            pictureViewTopCons.constant = (count == 0 ? 0 : 8)
            
            
            //给底部工作条赋值
            commentBtn.setTitle(viewModel?.comment_text, for: .normal)
            repostBtn.setTitle(viewModel?.repost_text, for: .normal)
            ohYeah.setTitle(viewModel?.ohYeahText, for: .normal)
            
            
            //绑定被转发的的原创微博的正文
            retweetedLabel?.text = viewModel?.status?.retweeted_status?.text
            
        }
        
        
    
    }
    
    func rowHeight(viewmodels: CJStatusViewModel) -> CGFloat {
        
        //给属性赋值
        self.viewModel = viewmodels
        //注意提前刷新
        
        self.contentView.layoutIfNeeded()
        
        return toolBar.frame.maxY;
        
    }
    
    
    //根据图片的张数来计算配图视图的大小
    func changePictureViewSize(Count:Int)->(pSize:CGSize,itemSize:CGSize) {
        
        if Count == 0 {
            
            return (CGSize.zero,CGSize.zero)
        }
        if Count == 1 {
        
            //返回原比例的大小
            //根据urlString 去沙盒中获取缓存的图片
            //能否在这个地方就一定能确保图片已经被缓存到沙盒里
            let urlString = viewModel?.pictureInfos?.first?.wap_pic ?? ""
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: urlString)
            let imageSize = image!.size
            
            return (imageSize,imageSize)
        }
        
        
        if Count == 4 {
            
            let width = itemWidth * 2 + pictureCellMargin
            
            return (CGSize(width: width, height: width),CGSize(width: itemWidth, height: itemWidth))
            
        }
        
        //其他 1,2,3,5,6,7,8,9  -> 3 * n
        //已知个数 和 列数 来计算 函数
        let rowCount = CGFloat ((Count - 1) / 3 + 1)
        
        let height = rowCount * itemWidth + (rowCount - 1) * pictureCellMargin
        
        return (CGSize(width: maxWidth, height: height),CGSize(width: itemWidth, height: itemWidth))
        
        
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        //修改头像
        iconView.cornerRadius = 20
        
        //设置配图视图的布局对象  不能写死  单张item是可变的
       // pictureLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        
        //设置正文的最大长度
        retweetedLabel?.preferredMaxLayoutWidth = ScreenWidth - 16
        contentLabel.preferredMaxLayoutWidth = ScreenWidth - 16
        
        //设置间距
        //行间距 默认的间距: 10
        pictureLayout.minimumLineSpacing = pictureCellMargin
        pictureLayout.minimumInteritemSpacing = pictureCellMargin
        
        
        //设置cell的选择样式
        selectionStyle = .none
        
        
        
        
        
    }

    
}
